//
//  SearchItemsView.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/25.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import UIKit
import ReactiveCocoa
import YYCategories

class SearchItemsView: UIView {
    lazy var searchButton: UIButton = {
        let button = UIButton(type: .system)
        button.setImage(#imageLiteral(resourceName: "common_btn_search").withRenderingMode(.alwaysOriginal), for: .normal)
        button.addTarget(self, action: #selector(searchButtonClicked), for: .touchUpInside)
        return button
    }()

    lazy var rightButton: UIButton = {
        let button = UIButton(type: .system)
        button.addTarget(self, action: #selector(rightButtonClicked), for: .touchUpInside)
        return button
    }()

    lazy var searchBar: UISearchBar = {
        let searchBar = UISearchBar()
        searchBar.delegate = self
        searchBar.showsBookmarkButton = true
        searchBar.setImage(UIImage(), for: .search, state: .normal)
        searchBar.setImage(#imageLiteral(resourceName: "common_btn_search"), for: .bookmark, state: .normal)
        searchBar.barTintColor = .white
        let image = UIImage(color: .white, size: CGSize(width: 100, height: 22))
        let bgImage = image?.byRoundCornerRadius(8, borderWidth: 1 / UIScreen.main.scale, borderColor: UIColor(hexString: "#9b9b9b"))
        searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        searchBar.setSearchFieldBackgroundImage(bgImage!, for: .normal)
        searchBar.tintColor = UIColor(hexString: "#ff4259")
        searchBar.returnKeyType = .search
        searchBar.alpha = 0
        return searchBar
    }()

    /// 点击搜索按钮
    @objc private func searchButtonClicked() {
        UIView.animate(withDuration: 0.25, animations: {
            self.searchBar.alpha = 1
            self.searchButton.alpha = 0

        }) { (finished) in
            if finished {
                self.searchBar.becomeFirstResponder()
            }
        }
    }

    var rightButtonTapsAction: VoidBlock?

    /// 点击右侧按钮
    @objc private func rightButtonClicked() {
        rightButtonTapsAction?()
    }

    override var intrinsicContentSize: CGSize {
        return CGSize(width: 350, height: 30)
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        setup()
    }

    private func setup() {
        addSubview(rightButton)
        addSubview(searchButton)
        addSubview(searchBar)

        searchBar.setContentHuggingPriority(UILayoutPriority(200), for: .horizontal)
        searchBar.setContentCompressionResistancePriority(UILayoutPriority(200), for: .horizontal)

        rightButton.mas_makeConstraints { (make) in
            make?.right.centerY().equalTo()(self)
        }
        searchButton.mas_makeConstraints { (make) in
            make?.centerY.equalTo()(self)
            make?.right.equalTo()(rightButton.mas_left)?.offset()(-13)
        }
        searchBar.mas_makeConstraints { (make) in
            make?.centerY.equalTo()(self)
            make?.right.equalTo()(rightButton.mas_left)?.offset()(4)
            make?.left.equalTo()(self)
        }
    }
}

extension SearchItemsView: UISearchBarDelegate {

    func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {

        return true
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {



    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        UIView.animate(withDuration: 0.25, animations: {
            self.searchBar.alpha = 0
            self.searchButton.alpha = 1
        }) { (finished) in

        }
    }

}
